Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs suggestion: use "prepare" instead of "postinstall" #884

Closed
nathansmith opened this issue Feb 24, 2021 · 9 comments
Closed

Docs suggestion: use "prepare" instead of "postinstall" #884

nathansmith opened this issue Feb 24, 2021 · 9 comments

Comments

@nathansmith
Copy link
Contributor

nathansmith commented Feb 24, 2021

TL;DR = Suggestion for the documentation site

This is not a bug "issue," but I wanted to make a suggestion for the docs.

I think this could alleviate some confusion around postinstall and remove the necessity for pinst entirely.

Currently, the Husky v5 documentation reads like this.

To automatically have Git hooks enabled after install, edit package.json

// package.json
{
  "private": true,
  "scripts": {
    "postinstall": "husky install"
  }
}

Instead, that could be changed to this. Swap out posinstall for prepare.

Also, private does not matter because prepare runs the same regardless.

To automatically have Git hooks enabled after install, edit package.json

// package.json
{
  "scripts": {
    "prepare": "husky install"
  }
}

Longer explanation

I did a writeup for my coworkers, to explain our automated setup for configuring Husky v5.

Basically, this all runs via NPM with the prepare command and puts everything into place.

If this is helpful for the docs, feel free to repurpose it.

This prepare.js file runs after npm install, when pulling down dependencies.

{
  "scripts": {
    "prepare": "node ./scripts/prepare.js"
  }
}

Unlike postinstall, the prepare command does not run when a NPM package is installed as a dependency in another project.

Meaning, it will not run within a package when someone types this…

npm install your-npm-package

…to install it for their own project.

But it will run when working on your-npm-package and installing other dependencies locally. This replicates the behavior we had before with Husky v4.

Code within the prepare.js file looks like this.

// =======
// Import.
// =======

const { execSync } = require('child_process');
const { existsSync } = require('fs');

// ===========
// File paths.
// ===========

const FILE_COMMIT = './.husky/pre-commit';
const FILE_HUSKY = './.husky/_/husky.sh';

// =========
// Commands.
// =========

const CLI_COMMIT = 'npx husky add .husky/pre-commit "npx lint-staged"';
const CLI_HUSKY = 'npx husky install';

// ==============
// Husky install.
// ==============

if (!existsSync(FILE_HUSKY)) {
  global.console.log(CLI_HUSKY);
  execSync(CLI_HUSKY);
}

// ====================
// Add pre-commit hook.
// ====================

if (!existsSync(FILE_COMMIT)) {
  global.console.log(CLI_COMMIT);
  execSync(CLI_COMMIT);
}

Code within the conditional logic will create .husky/pre-commit if it does not yet exist.

That adds a snippet (instructions for Husky) at the top of the file, and then appends the actual text that we passed in.

#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
@blimmer
Copy link
Contributor

blimmer commented Feb 25, 2021

If this change is made to the docs, you'll probably also want to update the migration helper: https://github.com/typicode/husky-4-to-5/blob/30792c918d08c0ebb99e1ef8942d13be91b9dd3e/src/pkg.ts#L65

@typicode
Copy link
Owner

@nathansmith Looks great, thanks a lot for the suggestion and detailed explanation! 👍

@typicode
Copy link
Owner

typicode commented Mar 1, 2021

I've updated docs, it's a better approach for npm/pnpm/yarn v1 👍
However it seems like Yarn v2 doesn't support prepare https://yarnpkg.com/advanced/lifecycle-scripts

@typicode
Copy link
Owner

typicode commented Mar 2, 2021

I've added a dedicated section to the docs for Yarn v2.

Also husky init will detect Yarn v2 and use postinstall [+ pinst] instead of prepare.

infotexture added a commit to dita-ot/docs that referenced this issue Mar 2, 2021
Not required when Husky runs via prepare script (only for postinstall)

Amends 76df9bd per typicode/husky#884

Signed-off-by: Roger Sheen <roger@infotexture.net>
@typicode typicode closed this as completed Mar 3, 2021
chalkygames123 added a commit to chalkygames123/stylelint-config that referenced this issue Mar 20, 2021
mfranzke added a commit to mfranzke/patternlab-node that referenced this issue Mar 28, 2021
JosefBredereck added a commit to pattern-lab/patternlab-node that referenced this issue Apr 17, 2021
* refactor: updated husky and pretty-quick dependencies

and updated to the new config storage model and removed husky and pretty-quick from packages/core, as this should be possible to get triggered from root

* refactor: updated prettier dependency

and removed rules "trailingComma": "es5" as this is the new default

* fix: added missing comma

* refactor: regenerated yarn.lock file

* refactor(husky): made additional necessary changes for the installation

as well as moved husky to the devDependencies, as defined by the software doc itself

* refactor(pinst): remove that solution again

see typicode/husky#884

* refactor(code): integrated prettier feedback

that was enforced like this by eslint

* refactor(husky): updated husky and added pinst

Yarn 2 doesn't support prepare lifecycle script, so husky needs to be installed differently (this doesn't apply to Yarn 1 though)

* Revert "refactor(husky): updated husky and added pinst"

This reverts commit e834b42.

* refactor(husky): updated husky

Co-authored-by: Josef Bredreck <slime.games@outlook.de>
@andresmatasuarez
Copy link

There's an important caveat to the use of prepare (or postinstall for that matter) that's worth mentioning:

If you have installed husky as a devDependency (as is my case and is suggested in the official docs) and are running npm publish as part of a CI or release process or whatever, then it'd simply not work due to husky not found error.

I wouldn't want to resort to simply installing husky in the regular dependencies and I know there's a whole official section explaining ways to disable husky for specific environments, but I wanted to make people coming across this issue in the future aware of this finding

@david10sing
Copy link

I have package A (components library) where I use husky with prepare and I have another repository (Package B) where I use package A (from private git repository) as well as husky with prepare.

During yarn install, it looks like yarn tries to install package A because it has a prepare script and also install other deps from package B. It appears both installs happen together and causes issues with overwritting cache.

@patoi
Copy link

patoi commented Oct 9, 2021

I think the best solution to install deps for production, if you run npm ci --ignore-scripts --production
In this case, prepare will be ignored, and the husky devDeps doesn't break your build.

RTFM: https://docs.npmjs.com/cli/v7/commands/npm-ci#ignore-scripts

@haoadoreorange
Copy link

haoadoreorange commented Nov 2, 2021

There's an important caveat to the use of prepare (or postinstall for that matter) that's worth mentioning:

If you have installed husky as a devDependency (as is my case and is suggested in the official docs) and are running npm publish as part of a CI or release process or whatever, then it'd simply not work due to husky not found error.

I wouldn't want to resort to simply installing husky in the regular dependencies and I know there's a whole official section explaining ways to disable husky for specific environments, but I wanted to make people coming across this issue in the future aware of this finding

I second this, the actual best approach even for yarn classic is to have it in postinstall and use pinst. You'll also save yourself some times if ever you move to yarn 2.

Maybe you should make this the recommended setup on the doc.

@nextglabs
Copy link

There's an important caveat to the use of prepare (or postinstall for that matter) that's worth mentioning:
If you have installed husky as a devDependency (as is my case and is suggested in the official docs) and are running npm publish as part of a CI or release process or whatever, then it'd simply not work due to husky not found error.
I wouldn't want to resort to simply installing husky in the regular dependencies and I know there's a whole official section explaining ways to disable husky for specific environments, but I wanted to make people coming across this issue in the future aware of this finding

I second this, the actual best approach even for yarn classic is to have it in postinstall and use pinst. You'll also save yourself some times if ever you move to yarn 2.

Maybe you should make this the recommended setup on the doc.

Running yarn publish --ignore-scripts seems to solve this issue.

enricoemiro added a commit to enricoemiro/mailer that referenced this issue May 10, 2022
lnist added a commit to mangrovedao/mangrove-core that referenced this issue Nov 11, 2022
lnist added a commit to mangrovedao/mangrove-core that referenced this issue Nov 11, 2022
tsadler1988 added a commit to bbc/lrud-spatial that referenced this issue Jun 29, 2023
jordanholt added a commit to bbc/lrud-spatial that referenced this issue Jul 6, 2023
* Add CI workflows for PRs and publishing

* Do not specify Chromium path - use default (bundled)

* Scope npm to public @bbc

* Add @bbc scope to package-lock.json

* Do not build postinstall as this is done before publishing to npm

* Ignore scripts on publish as prepare is only used for dev environment

See typicode/husky#884 for context on use of prepare.

* Fix typo

---------

Co-authored-by: Tom Sadler <thomas.sadler@bbc.co.uk>
curbengh pushed a commit to curbengh/curbengh.github.io that referenced this issue Jan 7, 2024
- when installing from a git url, tsc command needs
to be in the "prepare" lifecycle script.
  - [when installing from a git url,] If the package being installed contains a prepare script, its dependencies and devDependencies will be installed, and the prepare script will be run, before the package is packaged and installed.
    - https://docs.npmjs.com/cli/v10/commands/npm-install
  - hexo uses prepare script to install husky git hook
    - typicode/husky#884
- Revert af212dd
- Reapply 844a785
technophile-04 added a commit to scaffold-eth/create-eth that referenced this issue Jun 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants